package com.xzc.kafka.action.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.errors.WakeupException;

import java.util.*;

public class _0ConsumerTest {

    public static void main(String[] args) {
        String topicName = "test-topic1";

        String groupID = "test-group";
        Properties props = new Properties();
        props.put("bootstrap.servers", "es1:19092,es2:19092,es3:19092");
        props.put("group.id", groupID);
//        props.put("enable.auto.commit", true);
        props.put("enable.auto.commit", false);
        props.put("auto.commit.interval.ms", "1000");
        props.put("auto.offset.reset", "earliest");
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList(topicName));
        wait1000(consumer);
//        justWait(consumer);
    }

    public static void wait1000(KafkaConsumer<String, String> consumer) {
        try {
            while (true) {
                //等待1000，可以让consumer醒过来，处理点其它事情
                ConsumerRecords<String, String> records = consumer.poll(1000);
                for (ConsumerRecord<String, String> record : records) {
                    System.out.printf("offset=%d key=%s val=%s\n",
                            record.offset(),
                            record.key(),
                            record.value());
                }
            }
        } finally {
            consumer.close();
        }
    }

    public static void justWait(KafkaConsumer<String, String> consumer) {
        try {
            while (true) {
                //无限等待 consumer.wakeup()其它线程可以调用这个让poll报出异常，但这个异常只能在下次执行到poll才会抛
                ConsumerRecords<String, String> records = consumer.poll(Long.MAX_VALUE);
                for (ConsumerRecord<String, String> record : records) {
                    System.out.printf("offset=%d key=%s val=%s\n",
                            record.offset(),
                            record.key(),
                            record.value());
                }
            }
        } catch (WakeupException e) {
        } finally {
            consumer.close();
        }
    }

    public static void commitByYourself(KafkaConsumer<String, String> consumer) {
        try {
            final int minBatchSize = 500;
            List<ConsumerRecord<String, String>> buffer = new ArrayList<>();
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(1000);
                for (ConsumerRecord<String, String> record : records) {
                    buffer.add(record);
                }
                if (buffer.size() >= minBatchSize) {
                    //insertIntoDb(buffer);
                    consumer.commitSync();
                    buffer.clear();
                }
            }
        } catch (WakeupException e) {
        } finally {
            consumer.close();
        }
    }

    public static void commitForPartition(KafkaConsumer<String, String> consumer) {
        try {
            while (true) {
                ConsumerRecords<String, String> records = consumer.poll(1000);
                for (TopicPartition partition : records.partitions()) {
                    List<ConsumerRecord<String, String>> partitionRecords = records.records(partition);
                    //一次消费了一个分区的数据
                    for (ConsumerRecord<String, String> record : partitionRecords) {
                        System.out.println(record.offset() + ":" + record.value());
                    }
                    long lastOffset = partitionRecords.get(partitionRecords.size() - 1).offset();
                    consumer.commitSync(Collections.singletonMap(partition, new OffsetAndMetadata(lastOffset + 1)));
                }
            }
        } catch (WakeupException e) {
        } finally {
            consumer.close();
        }
    }
}
